﻿Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports Mysoft.Map.Data
Imports MySoft.Map.Security
Imports Mysoft.Map.Caching
Imports MySoft.Map.Service


<WebService(Description:="Web服务说明：</br>为更新包管理工具提供对ERP系统的相关服务", [Namespace]:="http://www.mysoft.com.cn/erp3/updatepack")> _
Public Class UpdatePackWebService
    Inherits System.Web.Services.WebService

#Region " Web 服务设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Web 服务设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加您自己的初始化代码

    End Sub

    'Web 服务设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Web 服务设计器所必需的
    '可以使用 Web 服务设计器修改此过程。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: 此过程是 Web 服务设计器所必需的
        '不要使用代码编辑器修改它。
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    Public AuthenticationHeader As AuthHeader

#Region "安全验证"
    Private Function Authenticate(ByVal hed As AuthHeader, ByRef sRtn As String) As Boolean
        Return Authenticate(hed, sRtn, False)
    End Function

    Private Function Authenticate(ByVal hed As AuthHeader, ByRef sRtn As String, ByVal excuteSQL As Boolean) As Boolean
        If hed Is Nothing Then
            sRtn = "FAIL|缺少SoapHeader！"
            Return False
        Else
            Return hed.Authenticate(sRtn, excuteSQL)
        End If
    End Function
#End Region

#Region "WebService 方法"
    ''' <summary>
    ''' 执行更新SQL
    ''' </summary>
    ''' <param name="sqlScripts">SQL脚本(字符串数组)</param>
    ''' <returns></returns>
    ''' <remarks>
    ''' 异常信息格式:脚本序号?异常信息(序号为脚本在数组中的下标,从0开始.如果为-1,则是系统异常)
    ''' </remarks>
    <WebMethod(EnableSession:=True, Description:="执行更新SQL <br/>返回信息内容：结果（OK 或 Fail|异常信息)"), SoapHeader("AuthenticationHeader")> _
    Public Function ExcuteSql(ByVal sqlScripts As String()) As String
        Dim sRtn As String = String.Empty
        If Not Authenticate(AuthenticationHeader, sRtn, True) Then
            Return sRtn
        End If
        sRtn = UpdatePack.ExcuteSQL(sqlScripts)
        Return sRtn
    End Function

    ''' <summary>
    ''' 在线用户列表查询
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    <WebMethod(EnableSession:=True, Description:="在线用户列表查询 <br/>异常时返回Nothing"), SoapHeader("AuthenticationHeader")> _
    Public Function GetOnlineUsers() As DataSet
        Dim sRtn As String = String.Empty
        Dim dsReturn As DataSet
        If Not Authenticate(AuthenticationHeader, sRtn) Then
            Return Nothing
        End If
        Return OnlineUser.GetOnlineUserList()
    End Function

    ''' <summary>
    ''' 获取ERP运行状态
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    <WebMethod(EnableSession:=True, Description:="获取ERP运行状态  <br/>返回信息内容：结果（OK|运行状态 或 Fail|异常信息"), SoapHeader("AuthenticationHeader")> _
      Public Function GetRunningState() As String
        Dim sRtn As String = String.Empty
        If Not Authenticate(AuthenticationHeader, sRtn) Then
            Return sRtn
        End If
        Return RunningState.GetRunningState()
    End Function

    ''' <summary>
    ''' ERP运行状态设置
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    <WebMethod(EnableSession:=True, Description:="ERP运行状态设置"), SoapHeader("AuthenticationHeader")> _
    Public Function SetRunningState() As String
        Dim sRtn As String = String.Empty
        If Not Authenticate(AuthenticationHeader, sRtn) Then
            Return sRtn
        End If
        Return RunningState.SetRunningState
    End Function

    ''' <summary>
    ''' ERP维护状态设置
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    <WebMethod(EnableSession:=True, Description:="ERP维护状态设置"), SoapHeader("AuthenticationHeader")> _
    Public Function SetMaintenanceState() As String
        Dim sRtn As String = String.Empty
        If Not Authenticate(AuthenticationHeader, sRtn) Then
            Return sRtn
        End If
        Return RunningState.SetMaintenanceState
    End Function

    ''' <summary>
    ''' 获取服务器当前时间
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    <WebMethod(EnableSession:=True, Description:="ERP获取服务器当前时间 <br>异常时返回DateTime.MinValue"), SoapHeader("AuthenticationHeader")> _
    Public Function GetServerTime() As DateTime
        Dim sRtn As String = String.Empty
        If Not Authenticate(AuthenticationHeader, sRtn) Then
            Return DateTime.MinValue
        End If
        Return DateTime.Now
    End Function
#End Region

    ' Web 服务示例
    ' HelloWorld() 示例服务返回字符串 Hello World。
    ' 若要生成项目，请取消注释以下行，然后保存并生成项目。
    ' 若要测试此 Web 服务，请确保 .asmx 文件是起始页
    ' 并按 F5 键。
    '
    '<WebMethod()> _
    'Public Function HelloWorld() As String
    '   Return "Hello World"
    'End Function

End Class
